package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.api.user.GetUserInfo;
import com.cssw.fyzb.busi.service.acct.AcctService;
import com.cssw.fyzb.busi.service.acct.OutAcctService;
import com.cssw.fyzb.busi.service.system.SysParamService;
import com.cssw.fyzb.busi.service.team.TeamService;
import com.cssw.fyzb.busi.service.user.UserExtService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.acct.*;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;


/**
 * 获取果子提现信息
 *
 * asa
 */
@Function(description = "获取果子提现信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class GetGzCashInfo implements Action {

    @Autowired
    private AcctService acctService;

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;

    //新老用户注册时间规则
    @Value("${task.limitDate:2021-01-21 00:00:00}")
    private String limitDateStr;

    @Autowired
    private SysParamService sysParamService;

    @Autowired
    private OutAcctService outAcctService;

    @Autowired
    private UserExtService userExtService;

    private static final Logger logger = LoggerFactory.getLogger(GetUserInfo.class);
    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");

        try {

            Acct acct = acctService.queryAcctByUIdAndType(userId, AcctType.ACCT_TYPE_G);
            GzCashInfoVo vo = new GzCashInfoVo();
            vo.setUserGz(acct.getBalanceFee().setScale(4, BigDecimal.ROUND_DOWN));

            //新用户逻辑
//            if (isNewUser(user)){
//                SysParam sysParam = sysParamService.findByCode(SysParam.NEW_USER_GZ_PRICE);
//                vo.setValue(new BigDecimal(sysParam.getParamValue()).setScale(2).toString());
//            } else {
            Integer teamNum = teamService.getTeamNum(userId);
            //老用户走团队果子定价,所有用户都走同一个逻辑
            TeamGzPrice teamGzPrice = acctService.getTeamGzPriceByNum(teamNum);
            BigDecimal gzAddPrice = userExtService.getGzAddPrice(userId);
            BigDecimal gzPrice = null;
            BigDecimal monthLimitNum = null;
            if (null == teamGzPrice){
                gzPrice = BigDecimal.ZERO.add(gzAddPrice);
                vo.setValue(gzPrice.setScale(4, BigDecimal.ROUND_DOWN).toString());
                monthLimitNum = BigDecimal.ZERO;
                vo.setMonthLimitNum(monthLimitNum);
            } else {
                gzPrice = teamGzPrice.getPrice().add(gzAddPrice);
                vo.setValue(gzPrice.setScale(4, BigDecimal.ROUND_DOWN).toString());
                monthLimitNum = teamGzPrice.getMonthLimitNum();
                vo.setMonthLimitNum(monthLimitNum);
            }
//            }
            vo.setUserBankVo(convertBankVo(outAcctService.getBankByUserId(userId)));
            vo.setUserWechatVo(convertWechatVo(outAcctService.getWechatById(userId)));
            vo.setUserAliVo(convertAliVo(outAcctService.getAliById(userId)));
            vo.setGzCashTips("温馨提示：30天内可提现"+monthLimitNum+"个平安果");
           return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

    private UserAliVo convertAliVo(UserAli userAli) {
        UserAliVo userAliVo = null;
        if (userAli != null) {
            userAliVo = new UserAliVo();
            userAliVo.setUserId(userAli.getUserId());
            userAliVo.setAliNo(userAli.getAliNo());
            userAliVo.setRealName(userAli.getRealName());
            userAliVo.setReceQrCode(userAli.getReceQrCode());
        }
        return userAliVo;
    }

    private UserWechatVo convertWechatVo(UserWechat userWechat) {
        UserWechatVo userWechatVo = null;
        if (userWechat != null) {
            userWechatVo = new UserWechatVo();
            userWechatVo.setUserId(userWechat.getUserId());
            userWechatVo.setWechatNo(userWechat.getWechatNo());
            userWechatVo.setRealName(userWechat.getRealName());
            userWechatVo.setReceQrCode(userWechat.getReceQrCode());
        }
        return userWechatVo;
    }

    private UserBankVo convertBankVo(UserBank userBank) {
        UserBankVo userBankVo = null;
        if (userBank != null) {
            userBankVo = new UserBankVo();
            userBankVo.setBankId(userBank.getBankId());
            userBankVo.setUserId(userBank.getUserId());
            userBankVo.setCarNo(userBank.getCardNo());
            userBankVo.setRealName(userBank.getRealName());
            userBankVo.setBankName(userBank.getBankName());
            userBankVo.setOpenBank(userBank.getOpenBank());
            userBankVo.setDefaultFlag(userBank.getDefaultFlag());
            userBankVo.setProvince(userBank.getProvince());
            userBankVo.setCity(userBank.getCity());
            userBankVo.setBankCode(userBank.getBankCode());
        }
        return userBankVo;
    }

    private boolean isNewUser(User user){
        //判断是否新用户
        Date limitDate = TimeUtil.parse(this.limitDateStr, "yyyy-MM-dd HH:mm:ss");
        if (null != limitDate && null != user.getCreateTime() && user.getCreateTime().before(limitDate)) {
            return false;
        }
        return true;
    }
}
